package xm.chengdu.com.beijing_211;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import utils.FaSong;
import utils.SpfUtils;

public class Pin_CODERenActivity extends Activity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.et_gecao)
    EditText etGecao;
    @BindView(R.id.et_band)
    EditText etBand;
    @BindView(R.id.confirm)
    EditText confirm;
    @BindView(R.id.home)
    RoundButton home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pin);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }


    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("settingsuccess")) {
                finish();
            }else if (event.getType().equals("settingerr")) {
                finish();
            }


        }
    }


    @OnClick({R.id.img_back, R.id.home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;

            case R.id.home:
                String old = etGecao.getText().toString();
                String news = etBand.getText().toString().trim();
                String comm = confirm.getText().toString().trim();


                if (TextUtils.isEmpty(old)||old.length() != 6) {
                    Toast.makeText(this, "Please input the old password", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(news)||news.length() != 6) {
                    Toast.makeText(this, "Please input the new password", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(comm)||comm.length() != 6) {
                    Toast.makeText(this, "Please input the  confirm new password", Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean isfirst = (boolean) SpfUtils.get(Pin_CODERenActivity.this, "firstmima", true);
//                if (isfirst) {
                if (!old.equals("123456")) {
                         Toast.makeText(this, "Old password error！", Toast.LENGTH_SHORT).show();
                       return;
                     }
                    if (news.equals(comm)) {
                        String bding= FaSong.bangding("010203040506",comm);
                         MyEvent events4 = new MyEvent();

                        events4.setType("message");
                        events4.setContent(bding);
                        EventBus.getDefault().post(events4);


                    } else {
                        Toast.makeText(this, "fail,The new password and confirmation password are inconsistent.！", Toast.LENGTH_SHORT).show();
                    }


                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
